#include "StdAfx.h"
#include "xkRefPoint.h"
#include "xfRefMask.h"


xfRefMask::xfRefMask(void)
{
	m_pRefPoint = (xkRefPoint*)this;

	memset(m_szMaskFile,0,sizeof(m_szMaskFile));
	ClearMask();
}

xfRefMask::~xfRefMask(void)
{
}



int xfRefMask::ClearMask()
{
	memset(&m_xMaskHead,0,sizeof(m_xMaskHead));
	m_xBuffer.MemSet();

	return 0;
}


int xfRefMask::SetMask(void *pVoid,BOOL bSet)
{
	DWORD dwWidth  = (DWORD)m_pRefPoint->m_ImgBitInfo.nWidth;
	DWORD dwHeight = (DWORD)m_pRefPoint->m_ImgBitInfo.nHeight;
	DWORD dwSize   = dwWidth * dwHeight;

	zkAnalyMaskHead *pHead = &m_xMaskHead;

	if( NULL == pHead->pMask || dwWidth != pHead->dwWidth || dwHeight != pHead->dwHeight )
	{
		if( !m_xBuffer.Allocate(dwSize) )
		{
			return DD_ERR_NEWBUFFER;
		}
		m_xBuffer.MemSet();

		pHead->dwHeight   = dwHeight;
		pHead->dwWidth    = dwWidth;
	    pHead->pMask      = m_xBuffer.pBuffer;
	}


	zkAnalyClearArea *pArea = (zkAnalyClearArea*)pVoid;

	int nLeft   = pArea->x - pArea->nSize/2;
	int nRight  = nLeft + pArea->nSize;
	int nTop    = pArea->y - pArea->nSize/2;
	int nBottom = nTop + pArea->nSize;
	if( nLeft < 0 )
	{
		nLeft = 0;
	}
	if( nRight > (int)dwWidth )
	{
		nRight = dwWidth;
	}
	if( nTop < 0 )
	{
		nTop = 0;
	}
	if( nBottom > (int)dwHeight )
	{
		nBottom = dwHeight;
	}

	BYTE cValue = bSet ? 1:0;

	int nOffset = 0;
	for(int i=nTop;i<nBottom;i++)
	{
		for(int j=nLeft;j<nRight;j++)
		{
			nOffset = i*dwWidth+j;

			pHead->pMask[nOffset] = cValue;
		}
	}

	return 0;
}


#include "DDReadWriteFile.h"

int xfRefMask::SetMaskFile(char *pFileName)
{
	strcpy(m_szMaskFile,pFileName);

	return 0;
}

int xfRefMask::LoadMask()
{
	int nRet = -1;

	DDReadWriteFile xFile;
	zkAnalyMaskHead *pHead = &m_xMaskHead;

	do 
	{
		char *pFileName = m_szMaskFile;

		LONGLONG llFileSize = 0;

		if( 0 != xFile.DDCreateFile(pFileName,TRUE,&llFileSize) )
		{
			nRet = -1;
			break;
		}

		LONGLONG llFileOffset = 0;
		nRet = xFile.DDOperatorFile( (BYTE*)pHead,sizeof(zkAnalyMaskHead),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}
		if( !pHead->CheckVersion() )
		{
			nRet = -1;
			break;
		}

		DWORD dwWidth  = pHead->dwWidth;
		DWORD dwHeight = pHead->dwHeight;
		DWORD dwSize   = dwWidth * dwHeight;
		if( !m_xBuffer.Allocate(dwSize) )
		{
			nRet = DD_ERR_NEWBUFFER;
			break;
		}
		BYTE *pBuffer = m_xBuffer.pBuffer;
		nRet = xFile.DDOperatorFile( pBuffer,dwSize,&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		pHead->pMask   = (BYTE*)( pBuffer );

		return nRet;

	} while (FALSE);

	pHead->pMask   = NULL;

	memset(pHead,0,sizeof(zkAnalyMaskHead));

	return nRet;
}

int xfRefMask::SaveMask()
{
	int nRet = -1;

	if( NULL == m_xBuffer.pBuffer )
	{
		return DD_ERR_PARAMETER;
	}

	DDReadWriteFile xFile;

	do 
	{
		char *pFileName = m_szMaskFile;

		LONGLONG llFileSize = 0;

		if( 0 != xFile.DDCreateFile(pFileName,FALSE,&llFileSize) )
		{
			nRet = -1;
			break;
		}	

		LONGLONG llFileOffset = 0;

		zkAnalyMaskHead *pHead = &m_xMaskHead;
		pHead->dwFileVersion = DD_ANALY_VERSION_2012;
		nRet = xFile.DDOperatorFile( (BYTE*)pHead,sizeof(zkAnalyMaskHead),&llFileOffset,FALSE);
		if( nRet != 0)
		{
			break;
		}

		DWORD dwWidth  = pHead->dwWidth;
		DWORD dwHeight = pHead->dwHeight;
		DWORD dwSize   = dwWidth * dwHeight;
		BYTE *pBuffer  = m_xBuffer.pBuffer;

		nRet = xFile.DDOperatorFile( pBuffer,dwSize,&llFileOffset,FALSE);
		if( nRet != 0)
		{
			break;
		}

	} while (FALSE);

	return nRet;
}